<?php
if(isset($javascript)):
    // load script in <head> section
    $javascript->link('scriptaculous/lib/prototype', false);
    $javascript->link('scriptaculous/src/scriptaculous', false);
    $javascript->link('modalbox/modalbox', false);
    echo $html->css('modalbox');
endif;?>

<script>
function newitem(decision){
    $('Decision['+decision+'][ItemShortName]').disabled=false;
    $('Decision['+decision+'][ItemName]').disabled=false;
    $('Decision['+decision+'][Item]').disabled=true;
}

function existitem(decision){
    $('Decision['+decision+'][ItemShortName]').disabled=true;
    $('Decision['+decision+'][ItemName]').disabled=true;
    $('Decision['+decision+'][Item]').disabled=false;
}
function reloaditems(){
    var url='<?php echo $html->url(array('controller'=>'ajaxes','action'=>'latestitem'))?>/'+<?php echo $dcommittee['Committee']['id']; ?>;
    var dajax=new Ajax.Request(url,{onSuccess:refreshitems});
}
function refreshitems(response){
    items=response.responseText.split(',');
    dselector=$$('.itemlist');
    for(var ssindex=0,sslen=dselector.length; ssindex<sslen; ++ssindex){
        dselector[ssindex].options[dselector[ssindex].options.length]=new Option(items[1],items[0]);
    }
}
</script>
<?php
$needcheck=configure::read('check_status');
__('Decisions',true); //for translation
$item_name = strlen($dcommittee['Committee']['item_name'])>2 ? __($dcommittee['Committee']['item_name'],true) : __('Project',true);
?>
<p class='contentmenu'>
    <?php 
    if($secretariat || ($auth_user['User']['superuser'] && $decision['Decision']['approved']!=$needcheck)){
        //open the update status access for all the time
        foreach($decision1['User'] as $user){
            echo '[ '.$html->link(sprintf(__("Add new %s user status ",true),$user['name']),array('committee'=>$dcommittee['Committee']['short_name'],'controller'=>'Userstatuses','action'=>'add','decisionid'=>$decision['Decision']['id'],'user_id'=>$user['id'])).' ]';
        }
        foreach($decision1['Group'] as $group){
            echo '[ '.$html->link(sprintf(__("Add new %s group status ",true),$group['name']),array('committee'=>$dcommittee['Committee']['short_name'],'controller'=>'Groupstatuses','action'=>'add','decisionid'=>$decision['Decision']['id'],'group_id'=>$group['id'])).' ]';
        }
    }elseif(!empty($needcheck) && $decision['Decision']['grouped']==0 && $decision['Decision']['approved']==0 || empty($needcheck) && !empty($minuteapproval)){
    //if the decision status was grouped, the status can't be updated
        //check the update status deadline
        if( 
            //no status deadline
            (date("Y/m/d",strtotime($decision['Decision']['status_deadline']))<='1970/01/01' 
                //decision no expire
                && (date("Y/m/d",mktime())<= date("Y/m/d",strtotime($decision['Decision']['deadline']))
                    //no decision deadline
                    || date("Y/m/d",strtotime($decision['Decision']['deadline']))<='1970/01/01'
                )
            )
            //got status deadline
            ||date("Y/m/d",mktime()) <= date("Y/m/d",strtotime($decision['Decision']['status_deadline']))
        ){
            foreach($decision1['User'] as $user) :
                if(isset(${'allow_add_user_status_'.$user['id']})) :
                    echo '[ '.$html->link(sprintf(__("Add new %s user status ",true),$user['name']),array('committee'=>$dcommittee['Committee']['short_name'],'controller'=>'Userstatuses','action'=>'add','decisionid'=>$decision['Decision']['id'],'user_id'=>$user['id'])).' ]';
                endif; 
            endforeach;
            foreach($decision1['Group'] as $group) :
                if(isset(${'allow_add_group_status_'.$group['id']})) :
                    echo '[ '.$html->link(sprintf(__("Add new %s group status ",true),$group['name']),array('committee'=>$dcommittee['Committee']['short_name'],'controller'=>'Groupstatuses','action'=>'add','decisionid'=>$decision['Decision']['id'],'group_id'=>$group['id'])).' ]';
                endif; 
            endforeach;
        }else{
            echo __('The deadline for updating the status is now over',true);
        }
    }elseif($decision['Decision']['approved']==1 && !empty($needcheck)){
        echo __('The decision is already accepted by secretariat',true);
    }elseif(empty($minuteapproval) && empty($needcheck)){
        echo __('The minute has not been approved yet',true);
    }else{
        echo __('The decision statuses are already grouped by secretariat',true);
    }
            
    if((($secretariat)) && ($decision['Decision']['approved']==0 && (empty($needcheck) ||(!empty($needcheck) && ($allStatus || $decision['Decision']['grouped']==1))))){
        echo '[';
        echo $html->link(__('Add decision to minutes',true), array('committee'=>$dcommittee['Committee']['short_name'],'action'=>'approvedecision','id'=>$decision['Decision']['id']),null,sprintf(__('This will put the decision in the minutes for the chairperson to approve. Proceed?', true)),false); 
        echo ']';
    }?> 
</p>
<div class="decisions view">
    <h2 class="double">
        <?php If($decision['Decision']['approved']==1){ ?>
            <font color="green">
                <?php  
                    __('Decision');
                    echo ' ';
                    __('(In minutes)');
                ?>
            </font>
        <?php }else {
            __('Decision');
        } ?>
        <br/>
        <?php echo $decision['Meeting']['meeting_title'].' '.$decision['Meeting']['meeting_num']?>
    </h2>
    <table cellspacing='0' cellpadding='0' border='0' id="noborder">
        <tr>
            <td class="leftcolumn">
                <div class="contentsummary">
                    <ul>
                        <li><span class="viewtitle"><?php __('Committee'); ?>: </span><?php echo $decision['Committee']['name']; ?></li>
                        <li><span class="viewtitle"><?php __('Meeting Title'); ?>: </span><?php echo $decision['Meeting']['meeting_title'];?></li>
                        <li><span class="viewtitle"><?php __('Meeting No');?>:</span> <?php echo $html->link(strlen($decision['Meeting']['meeting_num'])?$decision['Meeting']['meeting_num']:$decision['Meeting']['meeting_title'],array('committee'=>$dcommittee['Committee']['short_name'],'controller'=>'meetings','action'=>'view',$decision['Meeting']['id']));?></li>
                        <li><span class="viewtitle"><?php __('Venue'); ?>: </span><?php echo $decision['Meeting']['venue']; ?></li>
                        <li><span class="viewtitle"><?php __('Meeting Date'); ?>: </span><?php echo date(Configure::read('date_format'),strtotime($decision['Meeting']['meeting_date'])); ?>
                        &nbsp;<?php echo date(Configure::read('time_format'),strtotime($decision['Meeting']['meeting_date'])); ?></li>
                        <li><span class="viewtitle"><?php __('Chairperson'); ?>: </span><?php echo $chairperson['Title']['short_name'].' '.$chairperson['User']['name']; ?></li>
                    </ul>
                </div>
                <fieldset id="decision">
                    <legend><?php __('Decision'); ?></legend>
                    <div class="fieldset-inside">
                        <div class='note'><?php __('This decision can only be added to minutes after all status have been accepted by the secretariat.')?></div>
                        <span class="viewtitle"><?php echo ucwords($item_name);?>:</span> <?php echo $html->link($decision['Item']['name'],array('committee'=>$dcommittee['Committee']['short_name'],'controller'=>'items','action'=>'view',$decision['Item']['id']));?><br/>
                        <span class="viewtitle"><?php __('Deadline');?>:</span> <?php if($decision['Decision']['deadline']!='0000-00-00')echo date(Configure::read('date_format'),strtotime($decision['Decision']['deadline']));?><br/>
                        <span class="viewtitle"><?php __('Description');?>:</span>
                        <?php echo $html->div('decision_text',$text->highlight($decision['Decision']['description'],$key));?>
                        <?php
                        // $imp=no. of implementor
                            $imp=0;
                            if(count($decision1['User'])):
                        ?>
                            <span class="viewtitle"><?php __('Individual Implementor');?>:</span> 
                            <?php 
                                echo "<ul>"; 
                                foreach($decision1['User'] as $user) { 
                                    $imp++;
                                    echo "<li>".$user['name']."</li>";
                                } 
                                echo "</ul>"; 
                            endif;
                            if(count($decision1['Group'])): 
                            ?>
                                <span class="viewtitle"><?php __('Group Implementor');?>:</span> 
                                <?php echo "<ul>"; 
                                foreach($decision1['Group'] as $group){ 
                                    $imp++;
                                    echo "<li>".$group['name']."</li>"; 
                                }
                                echo "</ul>"; 
                            endif; ?>

                    </div>
                </fieldset>
            </td>
            <td class="rightcolumn">
                <?php if(isset($decision['MultiFile'])){?>
                    <fieldset id="dfiles">
                        <legend><?php __('Files for this decision');?></legend>
                        <div class="fieldset-inside">
                            <?php
                                if(isset($decision['MultiFile']['additionalfiles'])){
                                    echo $this->element('multifile',array('multifiles'=>$decision['MultiFile']['additionalfiles']));
                                }
                            ?>
                        </div>
                    </fieldset>
                <?php } ?>
                <a name="comments"></a>
                <fieldset>
                    <legend>
                        <?php __('Comments for this decision'); ?>
                        <?php echo $comment->no_of_comments('Decision',$decision)? ': '.$comment->no_of_comments('Decision',$decision) : '' ?>
                    </legend>
                    <div class="fieldset-inside">
                        <div class='note'><?php __('Every comment made will be emailed to every implementor(s) of this decision.')?></div>
                        <?php echo $comment->disp_comment('Decision',$decision); ?>
                    </div>
                </fieldset>
            </td>
        </tr>
    </table>
    
    <?php /********************* STATUS TABLE GROUP/UNGROUP LINKS ******************************/ ?>
    <a name="status"></a>
    <div>
        <span class="viewtitle"><?php __('List of status')?></span>
        <div class='note'><?php __('Implementors can no longer update their status after the their status is accepted by the secretariat.')?></div>
        <?php if(!empty($needcheck)){
            // the status has been grouped
            if($decision['Decision']['grouped']==1){ 
                if((($secretariat) || $auth_user['User']['superuser'])&& $imp>1 && $decision['Decision']['approved']==0){
                    $singular=strtolower(Inflector::singularize($this->params['controller']));
                    echo '[ ';
                    echo $html->link(__('Ungroup the status',true),array('committee'=>$dcommittee['Committee']['short_name'],'controller'=>'decisions','action'=>'status',$singular.'id'=>$decision['Decision']['id'],'id'=>$decision['Decision']['id']),null,null,false);
                    echo ' ]' ;
                }
        ?>
            <table cellpadding='0' cellspacing='0' border='0' id="statuses">
                <tr>
                    <th width="95%"><?php __('Grouped Status')?></th>
                    <th width="5%"><?php __('Approved')?></th>
                </tr>
                <tr>
                    <td ><?php echo $decision['Decision']['status']; ?></td >
                    <td align=center>
                        <?php 
                            if ((($secretariat)||$auth_user['User']['superuser']) && $decision['Decision']['approved']==0){
                                echo $ajax->link($form->checkbox('approved'), array('committee'=>$dcommittee['Committee']['short_name'],'action'=>'approvedecision','id'=>$decision['Decision']['id']),array('complete'=>'location.reload()','escape'=>false),sprintf(__('Are you sure you want to add this decision to minutes?', true)),false);
                            }elseif($decision['Decision']['approved']==0){
                                echo $html->image('icons/pending.gif',array('title'=>__('Not accepted yet',true)));
                            }else {echo $html->image('icons/accepted.gif',array('title'=>__('Accepted',true)));}
                        ?>
                    </td>
                </tr>
            </table>
        <?php } ?>
        <?php 
            if((($secretariat)||$auth_user['User']['superuser'])&& $imp>1 && $decision['Decision']['approved']==0 && ($decision['Decision']['grouped']==0 )){
                $singular=strtolower(Inflector::singularize($this->params['controller']));
                echo '[ ';
                echo $html->link(__('Group the status',true),array('committee'=>$dcommittee['Committee']['short_name'],'controller'=>'decisions','action'=>'status',$singular.'id'=>$decision['Decision']['id'],'id'=>$decision['Decision']['id']),null,null,false);
                echo ' ]';
            }
        }
            $i=0;
        ?>
        
        <?php /********************* STATUS TABLE ******************************/ ?>
        <table cellpadding='0' cellspacing='0' border='0' id="statuses">
            <tr>
                <th width="10%"><?php __('Implementor')?></th>
                <th width="50%"><?php __('Status')?></th>
                <th width="10%"><?php __('Action Date')?></th>
                <th width="20%"><?php __('Comments')?></th>
                <th width="5%"><?php __('History')?></th>
                <?php if (!empty($needcheck)){?> <th width="5%"><?php __('Accepted')?></th> <?php } ?>
            </tr>
            <?php if(!count($decision1['User']) && !count($decision1['Group'])):?>
                <tr>
                    <td colspan='5'><?php __('Decision has been not assigned to anyone')?></td>
                </tr>
            <?php endif;?>
            <?php
                //================USER========================
                foreach($decision['Userstatus'] as $status): 
                    $class = null;
                    if ($i++ % 2 == 0) {
                        $class = ' class="altrow"';
                    }
            ?>
                    <tr<?php echo $class;?>>
                        <td><?php echo "<a name=\"user".$status['id']."\"></a>";?><?php echo $status['User']['name']?></td>
                        <td><?php echo $text->highlight(nl2br($status['description']),$key)?>
                            <br/>
                            <?php
                                if(isset($status['MultiFile']['additionalfiles'])) {
                                    echo "<span class='bold'>".__('Additional Files',true).": </span>"; 
                                    echo $this->element('multifile',array('multifiles'=>$status['MultiFile']['additionalfiles']));
                                }
                            ?>
                        </td>
                        <td><?php echo date(Configure::read('date_format'),strtotime($status['action_date']));?></td>
                        <td>
                            <?php 
                                if(isset($javascript)):echo $comment->add_button('Userstatus',$status); 
                                else:
                                    echo "<div>".$comment->add_form('Userstatus',$status).'</div><br/>';
                                    echo $comment->list_comment('Userstatus',$status);
                                endif;
                            ?>
                        </td>
                        <td>
                            <?php 
                                $historycount = 0;
                                    foreach ($user_history_status as $history) {
                                        if ($status['User']['id'] == $history['Userstatus']['user_id']) $historycount++;
                                    }
                                    echo $historycount.' &nbsp; [';
                                    echo $html->link(__('View',true),
                                        array('controller'=>'userstatuses','action'=>'view_history','committee'=>$dcommittee['Committee']['short_name'],'decisionid'=>$decision['Decision']['id'],'id'=>$status['user_id']),
                                        array('onclick'=>'Modalbox.show(this.href, {title:this.title,width:800});return false;','Title'=>__('View history',true))
                                    );
                                    echo ']';
                            ?>
                        </td>
                        <?php if (!empty($needcheck)){?>
                        <td>
                            <?php 
                                if($status['approved']==1){echo $html->image('icons/accepted.gif',array('title'=>__('Accepted',true)));}
                                else{
                                    if((($secretariat)|| $auth_user['User']['superuser'])&& $decision['Decision']['approved']==0){
                                        echo $ajax->link($form->checkbox('approved'), array('committee'=>$dcommittee['Committee']['short_name'],'action'=>'approveUserStatus','id'=>$status['id']),array('complete'=>'location.reload()','escape'=>false),sprintf(__('Implementors will not be able to update their status anymore after it is accepted. Proceed?', true)));
                                    }
                                    else{echo $html->image('icons/pending.gif',array('title'=>__('Not accepted yet',true)));    }
                                } ?>
                        </td> 
                        <?php } ?>
                    </tr>
                    
                <?php endforeach;?>
                <?php foreach($decision['User'] as $user):  
                    $class = null;
                    if ($i++ % 2 == 0) {
                        $class = ' class="altrow"';
                    }?>
                    <tr<?php echo $class;?>>
                        <td><?php echo $user['name']?></td>
                        <td><span class="merah"><?php __('Has not submitted status yet')?></span></td>
                        <td>-</td>
                        <td>-</td>
                        <td>-</td>
                        <?php if (!empty($needcheck)){?><td>-</td> <?php } ?>
                    </tr>
            <?php endforeach;?>
            <?php 
                //================GROUP========================
                foreach($decision['Groupstatus'] as $status): 
                    $class = null;
                    if ($i++ % 2 == 0) {
                        $class = ' class="altrow"';
                    }?>
                    <tr<?php echo $class;?>>
                        <td><?php echo "<a name=\"group".$status['id']."\"></a>";?><?php echo $status['Group']['name']?></td>
                        <td>
                            <?php
                                echo $text->highlight(nl2br($status['description']),$key).'<br/>';
                                if(isset($status['MultiFile']['additionalfiles'])) {
                                    echo "<span class='bold'>".__('Additional Files',true).": </span>"; 
                                    echo $this->element('multifile',array('multifiles'=>$status['MultiFile']['additionalfiles']));
                                }
                            ?>
                        </td>
                        <td><?php echo date(Configure::read('date_format'),strtotime($status['action_date']));?></td>
                        <td>
                            <?php 
                                if(isset($javascript)):echo $comment->add_button('Groupstatus',$status); 
                                else:
                                    echo "<div>".$comment->add_form('Groupstatus',$status).'</div><br/>';
                                    echo $comment->list_comment('Groupstatus',$status);
                               endif;
                            ?>
                        </td>
                        <td>
                            <?php 
                                $historycount = 0;
                                foreach ($group_history_status as $history) { 
                                    if ($status['Group']['id'] == $history['Groupstatus']['group_id']) $historycount++;
                                }
                                echo $historycount.' &nbsp; [';
                                echo $html->link(__('View',true),
                                    array('controller'=>'groupstatuses','action'=>'view_history','committee'=>$dcommittee['Committee']['short_name'],'decisionid'=>$decision['Decision']['id'],'id'=>$status['group_id']),
                                    array('onclick'=>'Modalbox.show(this.href, {title:this.title,width:800});return false;','Title'=>__('View history',true))
                                );
                                echo ']';
                            ?>
                        </td>
                        <?php if (!empty($needcheck)){?>
                        <td>
                            <?php 
                                if($status['approved']==1){echo $html->image('icons/accepted.gif',array('title'=>__('Accepted',true)));}
                                else{
                                    if((($secretariat) || $auth_user['User']['superuser'])&& $decision['Decision']['approved']==0){   
                                        echo $ajax->link($form->checkbox('approved'),array('committee'=>$dcommittee['Committee']['short_name'],'controller'=>'decisions','action'=>'approvegroupStatus','id'=>$status['id']),array('complete'=>'location.reload()','escape'=>false),sprintf(__('Implementors will not be able to update their status anymore after it is accepted. Proceed?', true)));
                                    }
                                    else{
                                        echo $html->image('icons/pending.gif',array('title'=>__('Not accepted yet',true)));
                                    }
                                }
                            ?>
                        </td>
                        <?php } ?>
                    </tr>
                <?php endforeach;?>
                <?php foreach($decision['Group'] as $group): 
                    $class = null;
                    if ($i++ % 2 == 0) {
                        $class = ' class="altrow"';
                    }
                ?>
                    <tr<?php echo $class;?>>
                        <td><?php echo $group['name']?></td>
                        <td><span class="merah"><?php __('Has not submitted status yet')?></span></td>
                        <td>-</td>
                        <td>-</td>
                        <td>-</td>
                        <?php if (!empty($needcheck)){?><td>-</td> <?php } ?>
                    </tr>
                <?php endforeach;?>
        </table>
</div>
<?php echo $form->button(
	__('Back', true), 
	array(
		'type'=>'button',
		'onclick' => "location.href='".$html->url(array('committee'=>$dcommittee['Committee']['short_name'],'controller'=>'meetings','action'=>'view',$decision['Decision']['meeting_id']))."'"
	)
); 
?>

